/*
 * Created by kopper on 31.01.15 18:44
 * (C) Copyright 2015 kopperek@gmail.com
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-2.1.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

package app.android.kopper.foramemo.util;

import android.content.ContentValues;

import org.xmlpull.v1.XmlPullParser;

import java.math.BigDecimal;
import java.util.Date;

public class Word {
    private Long id;
    private String word;
    private Long r1;
    private Long r2;
    private BigDecimal ep;
    private Long repNo;
    private Boolean enable;
    private Long firstOccurrence;
    private Long lastAnswer;
    private Long nextOccurrence;
    private Long nextCalculatedOccurrence;
    private Date added;

    public Word(Long id, String word, Long r1, Long r2, BigDecimal ep, Long repNo, Boolean enable, Date firstOccurrence, Date lastAnswer, Date nextOccurrence, Long nextCalculatedOccurrence, Date added) {
        this.id = id;
        this.word = word;
        this.r1 = r1;
        this.r2 = r2;
        this.ep = ep;
        this.repNo = repNo;
        this.enable = enable;
        this.firstOccurrence = ForaMemoDbHelper.normalizeDate(firstOccurrence);
        this.lastAnswer = ForaMemoDbHelper.normalizeDate(lastAnswer);
        this.nextOccurrence = ForaMemoDbHelper.normalizeDate(nextOccurrence);
        this.nextCalculatedOccurrence = nextCalculatedOccurrence;
        this.added = added;
    }

    public Word(String word, BigDecimal ep, Date added) {
        this.word = word;
        this.ep = ep;
        this.added = added;
        this.enable=Boolean.TRUE;
    }

    public Word(XmlPullParser parser) {
        this.word = parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_WORD);
        this.r1 = stringToLong(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_R1));
        this.r2 = stringToLong(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_R2));
        this.ep = stringToBigDecimal(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_EP));
        this.repNo = stringToLong(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_REP_NO));
        this.enable = stringToBoolean(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE));
        this.firstOccurrence = stringToLong(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_FIRST_OCCURRENCE));
        this.lastAnswer = stringToLong(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_LAST_ANSWER));
        this.nextOccurrence = stringToLong(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE));
        this.nextCalculatedOccurrence = stringToLong(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_CALCULATED_OCCURRENCE));
        this.added = stringToDate(parser.getAttributeValue(null,ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ADDED));
    }

    private Date stringToDate(String value) {
        Long dateValue=stringToLong(value);
        return(dateValue==null?null:new Date(dateValue.longValue()));
    }

    private Long stringToLong(String value) {
        if(value==null||value.length()==0||"null".equals(value))
            return(null);
        return(new Long(value));
    }
    private BigDecimal stringToBigDecimal(String value) {
        if(value==null||value.length()==0||"null".equals(value))
            return(null);
        return(new BigDecimal(value));
    }

    private Boolean stringToBoolean(String value) {
        return Boolean.valueOf("1".equals(value));
    }

    @Override
    public String toString() {
        return "Word{" +
                "id=" + id +
                ", word='" + word + '\'' +
                ", r1=" + r1 +
                ", r2=" + r2 +
                ", ep=" + ep +
                ", repNo=" + repNo +
                ", enable=" + enable +
                ", firstOccurrence=" + firstOccurrence +
                ", lastAnswer=" + lastAnswer +
                ", nextOccurrence=" + nextOccurrence +
                ", nextCalculatedOccurrence=" + nextCalculatedOccurrence +
                ", added=" + added +
                '}';
    }

    public void save(ForaMemoDbHelper dbHelper) {
        try {
            if(id==null) { //insert
                ContentValues values = createValues();
                id = dbHelper.getWritableDatabase().insert(ForaMemoDbHelper.FeedEntry.TABLE_NAME, null, values);
                LogProxy.d("Inserted: " + values);
            } else { //update
                dbHelper.getWritableDatabase().update(ForaMemoDbHelper.FeedEntry.TABLE_NAME, createValues(), ForaMemoDbHelper.FeedEntry._ID+"=?",new String[] {String.valueOf(id)});
                LogProxy.d("Updated: " + id);
            }
        } catch (Exception e) {
            LogProxy.e("",e);
        }
    }

    public ContentValues createValues() {
        ContentValues values = new ContentValues();
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_WORD, word);
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_R1, r1);
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_R2, r2);
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_EP, extractDouble(ep));
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_REP_NO, repNo);
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ENABLE, extractBoolean(enable));
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_FIRST_OCCURRENCE, firstOccurrence);
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_LAST_ANSWER, lastAnswer);
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_OCCURRENCE, nextOccurrence);
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_NEXT_CALCULATED_OCCURRENCE, nextCalculatedOccurrence);
        values.put(ForaMemoDbHelper.FeedEntry.COLUMN_NAME_ADDED, extractDate(added));
        return values;
    }

    public static Long extractDate(Date date) {
        return date==null?null:date.getTime();
    }

    public static Long extractNormalizedDate(Date date) {
        return date==null?null:ForaMemoDbHelper.normalizeDate(date);
    }

    public static Integer extractBoolean(Boolean value) {
        return value==null?null:value?1:0;
    }

    public static Double extractDouble(BigDecimal bigDecimal) {
        return bigDecimal==null?null:bigDecimal.doubleValue();
    }

    public Long getId() {
        return id;
    }

    public String getWord() {
        return word;
    }

    public Long getR1() {
        return r1;
    }

    public Long getR2() {
        return r2;
    }

    public BigDecimal getEp() {
        return ep;
    }

    public Long getRepNo() {
        return repNo;
    }

    public Boolean getEnable() {
        return enable;
    }

    public Date getFirstOccurrence() {
        return firstOccurrence==null?null:new Date(firstOccurrence);
    }

    public Date getLastAnswer() {
        return lastAnswer==null?null:new Date(lastAnswer);
    }

    public Date getNextOccurrence() {
        return nextOccurrence==null?null:new Date(nextOccurrence);
    }

    public Long getNextCalculatedOccurrence() {
        return nextCalculatedOccurrence;
    }

    public Date getAdded() {
        return added;
    }

    public void setR1(Long r1) {
        this.r1 = r1;
    }

    public void setR2(Long r2) {
        this.r2 = r2;
    }

    public void setEp(BigDecimal ep) {
        this.ep = ep;
    }

    public void setRepNo(Long repNo) {
        this.repNo = repNo;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public void setFirstOccurrence(Date firstOccurrence) {
        this.firstOccurrence = ForaMemoDbHelper.normalizeDate(firstOccurrence);
    }

    public void setLastAnswer(Date lastAnswer) {
        this.lastAnswer = ForaMemoDbHelper.normalizeDate(lastAnswer);
    }

    public void setNextOccurrence(Date nextOccurrence) {
        this.nextOccurrence = ForaMemoDbHelper.normalizeDate(nextOccurrence);
    }

    public void setNextCalculatedOccurrence(Long nextCalculatedOccurrence) {
        this.nextCalculatedOccurrence = nextCalculatedOccurrence;
    }
}
